
package ro.qi.framework.sys.i18n.locales.entities;

import java.util.StringTokenizer;

import ro.qi.framework.sys.entities.Entity;

/**
 * Defines a system locale
 */
public class Locale extends Entity {

	public Locale() {
		super();
	}

	public Locale(int id) {
		super();
//		System.out.println("Locale(int id)");
//		CallDetective cd = new CallDetective();
//		System.out.println("caller 0: " + cd.findCaller(0));
//		System.out.println("caller 1: " + cd.findCaller(1));
//		System.out.println("caller 2: " + cd.findCaller(2));
//		System.out.println("caller 3: " + cd.findCaller(3));
//		System.out.println("caller 4: " + cd.findCaller(4));
//		System.out.println("caller 5: " + cd.findCaller(5));
//		System.out.println("caller 6: " + cd.findCaller(6));
//		System.out.println("caller 7: " + cd.findCaller(7));

		setId(id);
		select();
	}

	public Locale(String code) {
		super();
		setCode(code);
		select();
	}

	public Locale(String language, String country, String variant) {
		super();
		setLanguage(language);
		setCountry(country);
		setVariant(variant);
		select();
	}

	public void setId(int value) {
		setValue("id", value);
	}

	public int getId() {
		return getIntValue("id");
	}

	public void setLanguage(String value) {
		setValue("language", value);
	}

	public String getLanguage() {
		return getStringValue("language");
	}

	public void setCountry(String value) {
		setValue("country", value);
	}

	public String getCountry() {
		return getStringValue("country");
	}

	public void setVariant(String value) {
		setValue("variant", value);
	}

	public String getVariant() {
		return getStringValue("variant");
	}

	public void setPriority(int value) {
		setValue("priority", value);
	}
	
	public int getPriority() {
		return getIntValue("priority");
	}

	public void setPublished(boolean value) {
		setValue("published", value);
	}

	public boolean isPublished() {
		return getBooleanValue("published");
	}

	public void setName(String value) {
		setValue("name", value);
	}

	public String getName() {
		return getStringValue("name");
	}

	public void setCode(String value) {
		StringTokenizer stringToken = new StringTokenizer(value, "_");

		int i = 0;

		while (stringToken.hasMoreTokens()) {

			if (i == 0) { 
				setLanguage(stringToken.nextToken());
			}
			
			if (i == 1) {
				setCountry(stringToken.nextToken());
			}

			if (i == 2) {
				setVariant(stringToken.nextToken());
			}

			i++;
		}
	}

	public String getCode() {

		StringBuffer name = new StringBuffer();

		if (getLanguage() != null && getLanguage().length() > 0) {
			name.append(getLanguage().toLowerCase());
		}

		if (getCountry() != null && getCountry().length() > 0) {
			name.append("_" + getCountry().toUpperCase());
		} 

		// TODO: add toLowerCase or toUpperCase or make it case insensitive
		if (getVariant() != null && getVariant().length() > 0) {
			name.append("_" + getVariant());
		}

		return name.toString();
	}
	
	public java.util.Locale getLocale() {
		return new java.util.Locale(getLanguage(), getCountry(), getVariant());
	}

}
